// pages/home/index.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    refresherTriggered: false,
    userInfo: {},
    admin: null,
    show: false,
    value: "",
    artisan: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  async onConfirm() {
    try {
      Toast.loading({
        message: 'loading...',
        forbidClick: true,
        loadingType: 'spinner',
        mask: true
      });
      const res = await getApp().globalData.wxCloudRequest({
        type: "addAdmin",
        openId: wx.getStorageSync('idInfo')['openid'],
        avatar: this.data.userInfo.avatarUrl,
        gender: this.data.userInfo.gender,
        userName: this.data.userInfo.nickName,
        cheatStr: this.data.value
      });
      const {
        result: userRes
      } = await getApp().globalData.wxCloudRequest({
        type: "getUser",
        openId: wx.getStorageSync('idInfo')['openid']
      });
      this.setData({
        value: ""
      })
      getApp().globalData.idInfo = {
        ...wx.getStorageSync('idInfo'),
        admin: userRes.data.admin
      };
      wx.setStorageSync('idInfo', getApp().globalData.idInfo);
      this.onShow();
      Toast.clear();
    } catch (error) {
      console.log('error', error);
    }
  },
  fieldChange(e) {
    this.setData({
      value: e.detail
    })
  },
  getUserInfo(e) {
    if (e.detail.errMsg == "getUserProfile:ok") {
      getApp().globalData.userInfo = {
        ...e.detail.userInfo
      };
      wx.setStorageSync('userInfo', e.detail.userInfo)
      this.setData({
        userInfo: getApp().globalData.userInfo
      });
    }
  },
  async login() {
    const res = await wx.getUserInfo();
    getApp().globalData.wxCloudRequest({
      type: "getPhone",
      cloudID: res.cloudID
    }).then((info) => {})
  },
  bindrefresherrefresh(e) {
    this.setData({
      refresherTriggered: false
    })
  },
  bindscrolltolower() {

  },
  binddragstart() {

  },
  binddragend() {

  },
  async callPhone() {
    if (this.data.artisan) {
      try {
        await wx.makePhoneCall({
          phoneNumber: this.data.artisan,
        })
      } catch (error) {

      }

    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    const {
      artisanPhone
    } = wx.getStorageSync('dicts') || [];
    if (artisanPhone.length) {
      this.setData({
        artisan: artisanPhone[0].dictValue
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userInfo: wx.getStorageSync('userInfo') || {},
      admin: getApp().globalData.adminInfo[wx.getStorageSync('idInfo')['admin']]
    });
  },
  async longTap(e) {
    if (this.data.userInfo.nickName) {
      const res = await wx.showModal({
        title: "口令",
        // content:"请输入口令",
        cancelColor: '#333',
        editable: true,
        placeholderText: "请输入口令"
      })
      if (res.confirm) {
        this.setData({
          value:res.content
        })
        this.onConfirm()
      }
    }
  },
  onClose() {},
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})